package com.ktgj.flight.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.flight.domain.*;
import com.ktgj.flight.domain.vo.FlightOrderChangeVO;
import com.ktgj.flight.domain.vo.FlightRefundChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.flight.service.IFlightOrderChangeService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 机票预订_机票退改签废订单信息Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/change")
public class FlightOrderChangeController extends BaseController
{
    @Autowired
    private IFlightOrderChangeService flightOrderChangeService;

    /**
     * 查询机票预订_机票退改签废订单信息列表
     */
    @PreAuthorize(hasPermi = "flight:change:list")
    @GetMapping("/list")
    public TableDataInfo list(FlightOrderChange flightOrderChange)
    {
        startPage();
        List<FlightOrderChange> list = flightOrderChangeService.selectFlightOrderChangeList(flightOrderChange);
        return getDataTable(list);
    }

    /**
     * 导出机票预订_机票退改签废订单信息列表
     */
    @PreAuthorize(hasPermi = "flight:change:export")
    @Log(title = "机票预订_机票退改签废订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightOrderChange flightOrderChange) throws IOException
    {
        List<FlightOrderChange> list = flightOrderChangeService.selectFlightOrderChangeList(flightOrderChange);
        ExcelUtil<FlightOrderChange> util = new ExcelUtil<FlightOrderChange>(FlightOrderChange.class);
        util.exportExcel(response, list, "机票预订_机票退改签废订单信息数据");
    }

    /**
     * 获取机票预订_机票退改签废订单信息详细信息
     */
    @PreAuthorize(hasPermi = "flight:change:query")
    @GetMapping(value = "/{changeOrderId}")
    public AjaxResult getInfo(@PathVariable("changeOrderId") String changeOrderId)
    {
        return AjaxResult.success(flightOrderChangeService.selectFlightOrderChangeById(changeOrderId));
    }

    /**
     * 新增机票预订_机票退改签废订单信息
     */
    @PreAuthorize(hasPermi = "flight:change:add")
    @Log(title = "机票预订_机票退改签废订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightOrderChange flightOrderChange)
    {
        return toAjax(flightOrderChangeService.insertFlightOrderChange(flightOrderChange));
    }

    /**
     * 修改机票预订_机票退改签废订单信息
     */
    @PreAuthorize(hasPermi = "flight:change:edit")
    @Log(title = "机票预订_机票退改签废订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightOrderChange flightOrderChange)
    {
        return toAjax(flightOrderChangeService.updateFlightOrderChange(flightOrderChange));
    }

    /**
     * 删除机票预订_机票退改签废订单信息
     */
    @PreAuthorize(hasPermi = "flight:change:remove")
    @Log(title = "机票预订_机票退改签废订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{changeOrderIds}")
    public AjaxResult remove(@PathVariable Long[] changeOrderIds)
    {
        return toAjax(flightOrderChangeService.deleteFlightOrderChangeByIds(changeOrderIds));
    }

    /**
     * 退票申请
     * @param map
     * @return
     */
    @PostMapping("/orderChangeTickets")
    public AjaxResult orderChangeTickets(@RequestBody Map<String, Object> map) {
        Long orderId = Long.valueOf(map.get("orderId").toString());
        Map<String, Object> tickets = flightOrderChangeService.orderChangeTickets(orderId);
        return AjaxResult.success(tickets);
    }

    /**
     * 申请退票
     * @param map
     * @return
     */
    @PostMapping("/insertOrderChangeTickets")
    public AjaxResult insertOrderChangeTickets(@RequestBody Map<String, Object> map) {
        String refundReason = String.valueOf(map.get("refundReason"));
        Long orderId = Long.valueOf(map.get("orderId").toString());
        String pdfContent = String.valueOf(map.get("pdfContent"));
        return toAjax(flightOrderChangeService.insertOrderChangeTickets(refundReason, orderId, pdfContent));
    }
    /**
     * 机票退改签详情（查询机票订单的列表）
     */
    @PostMapping("/FlightRefundChange")
    public  AjaxResult flightReturnChange(@RequestBody FlightRefundChange flightRefundChange)  {
        startPage();
        return AjaxResult.success(flightOrderChangeService.selectFlightReturnChange(flightRefundChange));
    }

    /**
     * 退改签订单列表
     * @param tickets
     * @return
     */
    @PostMapping("/queryOrderChangeList")
    public TableDataInfo queryOrderChangeList(@RequestBody FlightOrderChangeTicketsDto tickets) {
        startPage();
        List<FlightOrderChangeVO> changeList = flightOrderChangeService.queryOrderChangeList(tickets);
        return getDataTable(changeList);
    }

    /**
     * 退改签订单详情
     * @param map
     * @return
     */
    @PostMapping("/queryOrderChange")
    public AjaxResult queryOrderChange(@RequestBody Map<String, Object> map) {
        String changeOrderId = String.valueOf(map.get("changeOrderId"));
        FlightChangeOrderVO orderChange = flightOrderChangeService.queryOrderChange(changeOrderId);
        if (orderChange != null)
            return AjaxResult.success(orderChange);
        return AjaxResult.error("系统错误");
    }

    /**
     * 机票改签详情
     * */
    @PostMapping("/selectOrderChangeDetails")
    public AjaxResult selectOrderChangeDetails(@RequestBody Map<String, Object> map){
        if (map.get("changeOrderId") != null && !map.get("changeOrderId").equals("")){
            String changeOrderId = String.valueOf(map.get("changeOrderId"));
            OrderFlightDetails orderFlightDetails;
            try {
                orderFlightDetails = flightOrderChangeService.selectOrderChangeDetails(changeOrderId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            return AjaxResult.success(orderFlightDetails);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 退票订单列表
     * */
    @PostMapping("/refundList")
    public List<FlightOrderChange> refundList(@RequestBody Map<String, Object> map) {
        String airline = map.get("airline").toString();
        String start = map.get("start").toString();
        String end = map.get("end").toString();
        List<FlightOrderChange> flightOrderInfoTickets = flightOrderChangeService.refundList(airline, start, end);
        return flightOrderInfoTickets;
    }

    /**
     * 会员手机号获取机票退票单
     * */
    @PostMapping("/refundForm")
    public AjaxResult refundForm(@RequestBody Map<String, Object> map){
        String phone = map.get("phone").toString();
        List<FlightOrderChange> flightOrderChangeList = flightOrderChangeService.refundForm(phone);
        return AjaxResult.success(flightOrderChangeList);
    }
}
